All files / varjoliitokauppa/app/api/admin/reviews/[id] route.ts

63.33% Statements 19/30
42.85% Branches 6/14
100% Functions 2/2
73.07% Lines 19/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                            1x 1x   1x 1x 1x     1x 1x 1x   1x 1x     1x   1x         1x                       1x 1x   1x 1x   1x       1x            
import { NextResponse } from 'next/server'
import { Prisma } from '@prisma/client'
import { prisma } from '@/lib/prisma'
import { checkAdminAuth } from '@/lib/auth-check'
import { validateRating, validateReviewStatus, ValidationError } from '@/lib/validation'
 
type Params = {
  params: Promise<{
    id: string
  }>
}
 
// PUT /api/admin/reviews/[id] - Update review (Admin only)
export async function PUT(request: Request, { params }: Params) {
  const authError = await checkAdminAuth()
  Iif (authError) return authError
 
  try {
    const { id } = await params
    const body = await request.json()
 
    // Build update data object - only include provided fields with validation
    const updateData: Prisma.ReviewUpdateInput = {}
    Eif (body.status !== undefined) {
      updateData.status = validateReviewStatus(body.status)
    }
    Iif (body.author !== undefined) updateData.author = body.author
    Iif (body.rating !== undefined) {
      updateData.rating = validateRating(body.rating)
    }
    Iif (body.text !== undefined) updateData.text = body.text
 
    const review = await prisma.review.update({
      where: { id },
      data: updateData,
    })
 
    return NextResponse.json(review)
  } catch (error) {
    console.error('Error updating review:', error)
    if (error instanceof ValidationError) {
      return NextResponse.json({ error: error.message }, { status: 400 })
    }
    return NextResponse.json({ error: 'Failed to update review' }, { status: 500 })
  }
}
 
// DELETE /api/admin/reviews/[id] - Delete a review (Admin only)
export async function DELETE(request: Request, { params }: Params) {
  const authError = await checkAdminAuth()
  Iif (authError) return authError
 
  try {
    const { id } = await params
 
    await prisma.review.delete({
      where: { id },
    })
 
    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Error deleting review:', error)
    return NextResponse.json({ error: 'Failed to delete review' }, { status: 500 })
  }
}